package cn.bingo.yunsj.quartz;

import cn.bingo.yunsj.quartz.SchedulerConfigParam.SchedulerConfig;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import org.springframework.stereotype.Component;

@Component
public class SchedulerAllJob {
    @Autowired
    private SchedulerFactoryBean schedulerFactoryBean;

    @Autowired
    private Scheduler scheduler;
    /*
     * 此处可以注入数据库操作，查询出所有的任务配置
     */

    /**
     * 该方法用来启动所有的定时任务
     *
     * @throws SchedulerException
     */
    public void scheduleJobs() throws SchedulerException {
//        Scheduler scheduler = schedulerFactoryBean.getScheduler();
        /**
         *
         */
        scheduleJob2(scheduler);
    }

    /**
     * 配置定时器二Job2
     * 定时扫描将发送贺卡
     * @param scheduler
     * @throws SchedulerException
     */
    private void scheduleJob2(Scheduler scheduler) throws SchedulerException {
        JobDetail jobDetail = JobBuilder.newJob(SecondJob.class).withIdentity("job2", "group1").build();
        CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(SchedulerConfig.GREETING_CARD_SCAN);
        // 发送贺卡定时策略
        CronTrigger cronTrigger = TriggerBuilder.newTrigger().withIdentity("trigger2"
                , "group1").withSchedule(scheduleBuilder).build();
        scheduler.scheduleJob(jobDetail, cronTrigger);
    }
}